
const Router = require('koa-router');
const router = new Router();
const Tool = require("./tool");
const fs = require("fs")
//数据库
const { resolve } = require('path');
const { stringify } = require('querystring');
var mysql = require('mysql');
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '123456',
  database: 'teaching'
});
connection.connect();

//主路径
router.get('/', async (ctx, next) => {
  // ctx.body = "服务器主要路径"
  this.body = await ctx.render('index');
});

//用户登录接口
router.post('/login', async (ctx, next) => {
  const user_name = ctx.request.body.用户名
  const user_pwd = ctx.request.body.密码
  // console.log(user_name, user_pwd)

  var sql = "select * from t_user where account = '" + user_name + "' and pwd= '" + user_pwd + "'"
  // console.log(sql)
  ctx.body = await Tool.xxx(sql)
});


//添加用户接口
router.post('/add_user', async (ctx, next) => {

  const user_name = ctx.request.body.用户名
  const user_pwd = ctx.request.body.密码

  ctx.body = await new Promise((resolve, reject) => {

    var sql = "select * from t_user where account = '" + user_name + "' "
    var sql1 = "insert into t_user (account,pwd,user_id) value(" + user_name + "," + user_pwd + "," + user_name + ")"
    connection.query(sql, function (error, results, fields) {
      if (results[0] != undefined) {
        // console.log("账号已存在")
        resolve("账号已成在")
      } else {
        connection.query(sql1, function (error, results, fields) {
          resolve("账号注册成功")
        })
      }
    })

  });
})

//查询接口
router.post('/query_data', async (ctx, next) => {
  ctx.body = await new Promise((resolve, reject) => {
    const data = ctx.request.body
    const user_id = data.userID
    let sql = "select * from  t_user where user_id ='" + user_id + "'"
    connection.query(sql, function (error, results, fields) {
      if (error != null) {
        resolve(error)
      } else {
        resolve(results)
      }
    });
  })
})

//用户个性签名修改
router.post('/signature', async (ctx, next) => {

  const data = ctx.request.body
  let user_id = data.userId
  let signature = data.data
  let data_length = data.data.length

  if (data_length <= 0) {
    ctx.body = "不能为空"
  } else {
    ctx.body = await new Promise((resolve, reject) => {
      let sql = "update t_user set  signature= '" + signature + "' where user_id=" + user_id + ""

      connection.query(sql, function (error, results, fields) {
        if (error != null) {
          resolve(error)
        } else {

          resolve(results)

        }
      });

    })
  }

});
//周报上传接口

router.post('/weekly',async(ctx,next)=>{
  let data=ctx.request.body.data
  let calendar=data.calendar
  let content=data.content
  let rate=data.rate
  let class_name=data.class_name
  let user_id=ctx.request.body.user_id
  let user_name=ctx.request.body.user_name
  let sql="insert into zp_weekly(zp_time,zp_rate,zp_content,zp_class,user_id,zp_people)  values('"+calendar+"','"+rate+"','"+content+"','"+class_name+"','"+user_id+"','"+user_name+"')"
  ctx.body=await new Promise((resolve,reject)=>{
    connection.query(sql,function(error,results,fields){
      if(error!=null){
        resolve(error)
      }else{
        // console.log(results)
        resolve(results)
      }
    })
  })
})
//周报查询接口
router.post('/weekly_inquiry',async(ctx,next)=>{
     const user_id =ctx.request.body.userID
    //  console.log(user_id)
    let sql = "select * from zp_weekly where user_id ='" + user_id + "'"
    ctx.body=await new Promise((resolve,reject)=>{
      connection.query(sql,function(error,results,fields){
        if(error !=null){
            resolve(error)
        }else{
            resolve(results)
        }
    })
    })
    
    // ctx.body="34567"
})
>>>>>>> ac9e53d8605c213a936286368c21f2718d5413e4

//班级修改
router.post('/change_class', async (ctx, next) => {
  const data = ctx.request.body
  let user_id = data.userId
  let u_class = data.data
  let sql = "update t_user set  u_class= '" + u_class + "' where user_id=" + user_id + ""
  ctx.body = await new Promise((resolve, reject) => {
    connection.query(sql, function (error, results, fields) {
      if (error != null) {
        resolve(error)
      } else {
        resolve(results)
      }
    })
  })

});

//图像上传
router.post('/updata_img', async (ctx, next) => {
  ctx.body = await new Promise((resolve, reject) => {
    const files = ctx.request.files.image
    const user_id = ctx.request.body.user_id
    let file_name = files.name
    let file_path = files.path
    // let time = new Date().getTime()
    // let xm_time = Tool.getLocalTime(time)
    file_name = new Date().getTime() + file_name
    let r_stream = fs.createReadStream(file_path)
    let w_stream = fs.createWriteStream("./static/" + file_name)
    r_stream.pipe(w_stream)

    r_stream.on("error", (err) => {
      resolve("文件上传出错", err)
    })
    w_stream.on("error", (err) => {
      resolve("文件上传出错", err)
    })
    r_stream.on("close", function () {
      let head_path = "http://localhost:3000/" + file_name
      let sql_str = "update t_user set head_img = '" + head_path + "' where user_id=" + user_id + ""
      Tool.xxx(sql_str)
      resolve(head_path)
      // resolve("文件上传成功", {
      //   img_path: head_path
      // })
    })

  })

  // sql 语句
  // var sql = "update t_user set sign = '" + sign + "' where user_id = '" + user_id + "'"
  // 当前接口返回什么
  // console.log(sql_str);
  // ctx.body = await Tool.xxx(sql)


});


//修改性别
router.post('/xiaomi', async (ctx, next) => {
  const data = ctx.request.body
  let sex_num = data.data
  let user_id = data.userId
  ctx.body = await new Promise((resolve, reject) => {
    var sex_name = ""
    if (sex_num == 1) {
      sex_name = "男"
    } else {
      sex_name = "女"
    }
    let sql = "update t_user set  sex= '" + sex_name + "' where user_id=" + user_id + ""
    connection.query(sql, function (error, results, fields) {
      resolve(results)
    });
  })
});

//手机号修改
router.post("/change_phone", async (ctx, next) => {
  let data = ctx.request.body
  let phone = data.data
  let user_id = data.userId
  let sql = "update t_user set  cell_phone= '" + phone + "' where user_id=" + user_id + ""
  ctx.body = await new Promise((resolve, reject) => {
    Tool.cheeked_phone(phone).then((res) => {
      if (res == "验证成功") {
        connection.query(sql, function (error, results, fields) {
          if (error != null) {
            resolve(error)
          } else {
            resolve(results)
          }
        });
      } else {
        resolve("验证失败")
      }
    })
  })
})
//修改用户密码
router.post("/changepassword", async (ctx, next) => {
  let data = ctx.request.body
  // let phone_data = data.data
  let user_id = data.user_id
  let old_pwd = data.data.旧密码
  let new_pwd = data.data.新密码
  let sql = "update t_user set  pwd='" + new_pwd + "' where user_id = '" + user_id + "' and  pwd ='" + old_pwd + "' "
  ctx.body = await new Promise((resolve, reject) => {
    if (old_pwd == new_pwd) {
      resolve("旧密码不能给新密码一样")
    } else {
      connection.query(sql, function (error, results, fields) {
        if (error != null) {
          resolve(error)
        } else {
          resolve(results)
        }
      });
    }
  })
})


// 打卡查询 

router.post("/attendance", async (ctx, next) => {
  // console.log(11111111);
  // let data = ctx.request.body.params;
  let data = ctx.request.body
  let datatime = new Date().toLocaleString()
  console.log(datatime);


  let sql = "insert into t_attendance (t_name, t_time,t_status) values('" + data.username + "','" + datatime + "','" + data.attendance + "')"

  ctx.body = await Tool.yyy(sql)


})
router.post("/find_atten", async (ctx, next) => {
  // console.log(11111111);
  // let data = ctx.request.body.params;
  let data = ctx.request.body
  // console.log(data);


  let sql = "select * from t_attendance where t_name = '" + data.username + "'"
  ctx.body = await Tool.yyy(sql)

})


router.post("/del_atten", async (ctx, next) => {
  console.log(222222222);
  // let data = ctx.request.body.params;
  let data = ctx.request.body


  let sql = "DELETE FROM t_attendance WHERE t_id = '" + data.t_id + "'"
  ctx.body = await Tool.yyy(sql)
})




// 简历 的 操作

// 修改
router.post('/updata_education', async (ctx, next) => {
  const data = ctx.request.body
  console.log(data);
  let sql = "update t_resume set  jl_school = '" + data.school + "', jl_major = '" + data.major + "', jl_education = '" + data.education + "' where jl_id=" + data.user_id + ""
  ctx.body = await Tool.yyy(sql)
});


router.post('/update_advan', async (ctx, next) => {
  const data = ctx.request.body
  console.log(data);
  let sql = "update t_resume set jl_describe = '" + data.text + "' where jl_id=" + data.user_id + ""
  console.log(sql);
  ctx.body = await Tool.yyy(sql)
});

router.post('/update_job', async (ctx, next) => {
  const data = ctx.request.body
  console.log(data);
  let sql = "update t_resume set jl_status = '" + data.status + "',  jl_position = '" + data.position + "', jl_industry = '" + data.industry + "', jl_city = '" + data.city + "', jl_salary = '" + data.salary + "' where jl_id=" + data.user_id + ""
  ctx.body = await Tool.yyy(sql)
});




router.post('/sel_resume', async (ctx, next) => {
  const data = ctx.request.body
  console.log(data);
  let sql = "select * from t_resume where jl_id = '" + data.user_id + "'"
  ctx.body = await Tool.yyy(sql)
});







// connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
//   if (error) throw error;
//   console.log('The solution is: ', results[0].solution);
// });
exports.router = router